<?
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006, 2007 Russell Peter Phillips

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

include ('inc_head_db.php');

if (strtolower ($_POST ['btnSubmit']) == 'edit') {
	//Make up URL
	$sHost = $_SERVER['HTTP_HOST'];
	$sURI = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	$sFile = 'ic_form.php';
	header ("Location: http://$sHost$sURI/$sFile");
}
elseif (strtolower ($_POST ['btnSubmit']) == 'confirm') {
	$sDate = date ('Y-m-d');
	//Character details - check if character exists
	$sql = "SELECT * FROM bookings WHERE bkPlayerID = $PLAYER_ID";
	$result = mysqli_query ($link, $sql);
	//If player has not booked insert a new row
	if (mysqli_num_rows ($result) == 0) {
		$sql = "INSERT INTO bookings (bkPlayerID, bkDateICConfirmed) VALUES ($PLAYER_ID, '$sDate')";
		if (! mysqli_query ($link, $sql)) {
			$sWarn = "There was a problem confirming your IC details";
			LogError ("Error inserting new IC booking.\nPlayer ID: $PLAYER_ID");
		}
	}
	else {
		//Update existing row
		$sql = "UPDATE bookings SET bkDateICConfirmed = '$sDate' WHERE bkPlayerID = $PLAYER_ID";
		if (! mysqli_query ($link, $sql)) {
			$sWarn = "There was a problem confirming your IC details";
			LogError ("Error inserting new IC booking.\nPlayer ID: $PLAYER_ID");
		}
	}
	//Make up URL
	$sHost = $_SERVER['HTTP_HOST'];
	$sURI = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	$sFile = 'start.php?green=' . urlencode ('Your IC details have been confirmed');
	header ("Location: http://$sHost$sURI/$sFile");
}

//Get bookings details. Determine if player is booked
$sql = "SELECT * FROM bookings WHERE bkPlayerID = $PLAYER_ID";
$result = mysqli_query ($link, $sql);
$row = mysqli_fetch_assoc ($result);
$sIC = $row ['bkDateICConfirmed'];
if ($sIC == '' || $sIC == '0000-00-00')
	$bConfirmed = False;
else
	$bConfirmed = True;

include ('inc_head_html.php');
include ('inc_forms.php');

//Get existing details if there are any
$sql = "SELECT * FROM characters WHERE chPlayerID = $PLAYER_ID";
$result = mysqli_query ($link, $sql);
$row = mysqli_fetch_assoc ($result);
$sNotes = $row ['chNotes'];
$sOSP = $row ['chOSP'];
?>

<h1><?=TITLE?> - IC Details</h1>

<p>
<?
if ($bConfirmed)
	echo "Your IC information has been confirmed. You will not be able to change it until after the upcoming event.";
else
	echo "Please check all of the informatin carefully. If anything needs amended, please click the <b>Edit</b> button. If you are happy that everything is correct, please click the <b>Confirm</b> button. Once you have confirmed that the information is correct, it will be frozen until after the upcoming event.";
?>
</p>

<table><tr>
<td>Character Name:</td>
<td><?=htmlentities (stripslashes ($row ['chName']))?></td>
</tr><tr>
<td>Race &amp; Gender:</td>
<td><?=htmlentities (stripslashes ($row ['chRace']))?>, <?=htmlentities (stripslashes ($row ['chGender']))?></td>
</tr><tr>
<td>Group:</td>
<td>
<?
if ($row ['chGroupSel'] == ' Other (enter name below)')
	$sGroup = htmlentities (stripslashes ($row ['chGroupText']));
else
	$sGroup = htmlentities (stripslashes ($row ['chGroupSel']));
echo $sGroup;
?>
</td>
</tr><tr>
<td>Faction and Rank:</td>
<td><?=htmlentities (stripslashes ($row ['chFaction']))?>, Rank <?=htmlentities (stripslashes ($row ['chFactionRank']))?></td>
</tr><tr>
<td>Guild(s) &amp; Rank(s) (enter &quot;None&quot; if appropriate):</td>
<td><?=htmlentities (stripslashes ($row ['chGuilds']))?></td>
</tr><tr>
<td>Ancestor:</td><td><?=htmlentities (stripslashes ($row ['chAncestor']))?></td>
</tr><tr>
<td>Isle of usual residence:</td>
<td><?=htmlentities (stripslashes ($row ['chIsle']))?></td>
</tr><tr>
<td>NPC Status:</td>
<td>
<?
if ($row ['chNPC'] == '1')
	echo "You are an NPC";
else
	echo "You are not an NPC";
?>
</td></tr>
<tr><td colspan = "2">&nbsp;</td></tr>
<tr><th colspan = "2">Skills</th></tr>
<?
//Get character's skills. Fill two arrays with the skills. These arrays can then be queried, avoiding repeated DB queries
$result = mysqli_query ($link, "SELECT * FROM skillstaken WHERE stPlayerID = '$PLAYER_ID'");
//$aiSkillID will hold the skill ID's. $aiPoints will hold the points spent
$aiSkillID = array ();
$aiPoints = array ();
while ($row = mysqli_fetch_assoc ($result)) {
	$iID = $row ['stSkillID'];
	$iPoints = $row ['stPoints'];
	$aiSkillID [] = $iID;
	$aiPoints [] = $iPoints;
}

$result = mysqli_query ($link, 'SELECT * FROM skills ORDER BY skID');
while ($row = mysqli_fetch_assoc ($result)) {
	//Find out if character has this skill
	$has = array_search ($row ['skID'], $aiSkillID);
	if ($has !== False) {
		//Character has the skill. Find out how many points they spent
		$points = $aiPoints [$has];
		echo "<tr><td colspan = '2'>" . htmlentities (stripslashes ($row ['skName']));
		//Display points spent if not fixed
		if ($row ['skMinCost'] != $row ['skMaxCost'])
			echo " - " . htmlentities (stripslashes ($points)) . " points";
		echo "</td></tr>";
	}
}
?>

<tr><td colspan = '2'>&nbsp;</td></tr>
<tr><td colspan = '2'><b>Notes</b><br>
<?
$sNotes = htmlentities (stripslashes ($sNotes));
if ($sNotes == '')
	echo "<i>None</i>\n";
else
	echo str_replace ("\n", "<br>", $sNotes);
?>
</td></tr>

<tr><td colspan = "2">&nbsp;</td></tr>
<tr><td colspan = '2'><b>OSPs, special items/powers/creatures:</b></td></tr>
<tr><td colspan = '2'>
<?
$sOSP = htmlentities (stripslashes ($sOSP));
if ($sOSP == '')
	echo "<i>None</i>\n";
else
	echo str_replace ("\n", "<br>", $sOSP);
?>
</td></tr>
<tr><td colspan = "2">&nbsp;</td></tr>
<form action = "ic_view.php" method = "post">
<?
if ($bConfirmed == False) {
	echo "<tr><td colspan = '2'><span class = 'warn'>Confirming will freeze details until after the upcoming event</span></td></tr>\n";
	echo "<tr><td colspan = '2'>&nbsp;</td></tr>\n";
	echo "<tr><td class = 'mid'><input type = 'submit' value = 'Edit' name = 'btnSubmit'></td>\n";
	echo "<td class = 'mid'><input type = 'submit' value = 'Confirm' name = 'btnSubmit'></td></tr>\n";
}
?>
</form>
</table>

<?
include ('inc_foot.php');
?>
